package com.xuecheng.media.task;

import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static com.xuecheng.media.utils.MinIOFileOperationUtils.getMimeType;

/**
 * 视频文件处理任务
 * @author Tang
 * @version 1.0
 * @date 2024/1/4 22:14
 */
@Slf4j
@Component
public class VideoFileProcessor {
    private final MediaProcessMapper mediaProcessMapper;
    private final static String VIDEO_X_MSVIDEO = "video/x-msvideo";
    public VideoFileProcessor(MediaProcessMapper mediaProcessMapper) {
        this.mediaProcessMapper = mediaProcessMapper;
    }

    public void addVideoFileProcessTask(MediaFiles mediaFiles) {
        String filename = mediaFiles.getFilename();
        String extension = filename.substring(filename.lastIndexOf("."));
        String minType = getMimeType(extension);
        if (VIDEO_X_MSVIDEO.equals(minType)) {
            mediaProcessMapper.insert(mediaFilesToMediaProcess(mediaFiles));
        } else {
            log.info("其他视频格式处理未适配");
        }
    }

    public MediaProcess mediaFilesToMediaProcess(MediaFiles mediaFiles) {
        MediaProcess mediaProcess = new MediaProcess();
        BeanUtils.copyProperties(mediaFiles, mediaProcess);
        mediaFiles = null;
        mediaProcess.setStatus("1");
        mediaProcess.setCreateDate(LocalDateTime.now());
        mediaProcess.setFailCount(0);
        mediaProcess.setUrl(null);
        return mediaProcess;
    }
}
